package com.zj.syzs.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zj.shouyou.R;
import com.zj.syzs.bean.MoneyBean;
import com.zj.syzs.utils.ImageDownLoader;

import java.util.List;

/**
 * Created by ZJ on 2015/6/16 0016.
 */
public class MoneyAdapter extends AppAdapter<MoneyBean> {
    private ImageDownLoader task;
    private DisplayImageOptions options;
    private MoneyBean moneyBean;

    public MoneyAdapter(List<MoneyBean> list, Context context) {
        super(list, context);
        task = new ImageDownLoader(context);
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.mipmap.ic_launcher);
        builder.showImageOnFail(R.mipmap.ic_launcher);
        builder.cacheInMemory(true).cacheOnDisk(true);
        builder.displayer(new RoundedBitmapDisplayer(20));
        options = builder.build();
    }

    @Override
    public View createView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        moneyBean = list.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_listview_money, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(moneyBean.getName());
        holder.count_dl.setText(moneyBean.getCount_dl() + "人下载");
        holder.size.setText(moneyBean.getSize());
        holder.dl_back_jifen.setText("奖" + moneyBean.getDl_back_jifen() + "Q币");

        holder.stat_ratingbar.setRating(Float.parseFloat(moneyBean.getScore())/2);

        ImageLoader.getInstance().displayImage(list.get(position).getIcon(), holder.icon, options);

        //如果图片存在内存中,直接返回 bitmap对象
        Bitmap bitmap = task.downLoader(holder.icon, new ImageDownLoader.ImageLoaderlistener() {
            @Override
            public void onImageLoader(Bitmap bitmap, ImageView imageView) {
                imageView.setImageBitmap(bitmap);
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        ImageView icon;
        TextView name, count_dl, size, dl_back_jifen;
        RatingBar stat_ratingbar;

        public ViewHolder(View itemView) {
            icon = (ImageView) itemView.findViewById(R.id.money_icon);
            name = (TextView) itemView.findViewById(R.id.name);
            count_dl = (TextView) itemView.findViewById(R.id.count_dl);
            size = (TextView) itemView.findViewById(R.id.size);
            dl_back_jifen = (TextView) itemView.findViewById(R.id.dl_back_jifen);
            stat_ratingbar = (RatingBar)itemView.findViewById(R.id.stat_ratingbar);
        }
    }
}
